﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Common;
using Sausage.Web.Code;
using Sausage.Web.DataModel;
using Sausage.Web.Models;
using Telerik.Web.Mvc;

namespace Sausage.Web.Domain
{
    public class DictionaryHelper
    {
        public static GridModel<DictionayItem> QueryDictionaryItem(JosnPagingQueryModel query)
        {
            using (var context = new SausageEntities())
            {
                return UIHelper.GetGridModelFromQueryModel<DictionayItem>(
                    context.DictionayItem.ToList().AsQueryable(),
                    query.page,
                    query.limit,
                    GetSortDescriptorFromQueryModel(query.SortModel),
                    GetSearchFilterFromQueryModel(query.FilterModel));
            }
        }


        private static List<IFilterDescriptor> GetSearchFilterFromQueryModel(IEnumerable<JosnQueryFilterModel> filterModels)
        {
            //just for the int equal filter.
            var filterList = new List<IFilterDescriptor>();
            foreach (var item in filterModels)
            {
                if (string.IsNullOrEmpty(item.value))
                    continue;

                filterList.Add(new FilterDescriptor(item.property, FilterOperator.IsEqualTo, int.Parse(item.value)));
            }
            return filterList;
        }

        private static List<SortDescriptor> GetSortDescriptorFromQueryModel(IEnumerable<JosnQuerySortModel> sortModels)
        {
            return UIHelper.GetCommonSortDescriptorFromSortModel(sortModels);
        }

        public static DeleteResult DeleteDictionaryItem(Guid id)
        {
            using (SausageEntities context = new SausageEntities())
            {
                DictionayItem dictionayItem = context.DictionayItem.FirstOrDefault(item => item.ItemID == id);

                if (null != dictionayItem)
                {
                    try
                    {
                        context.DictionayItem.DeleteObject(dictionayItem);
                        context.SaveChanges();
                    }
                    catch(UpdateException)
                    {
                        return DeleteResult.UsingError;
                    }
                    return DeleteResult.Ok;
                }
                return DeleteResult.Ok;
            }
        }

        public static void CreateDictionaryItem(DictionaryItemListRow data)
        {
            using (SausageEntities context = new SausageEntities())
            {
                DictionayItem dictionayItem = new DictionayItem()
                                                  {
                                                      DictionayID = data.DictionayID,
                                                      ItemID = Guid.NewGuid(),
                                                      ItemName = data.ItemName,
                                                      ItemPinYin = data.ItemPinYin,
                                                      ItemValue = data.ItemValue,
                                                      TheOrder = data.TheOrder
                                                  };

                context.AddToDictionayItem(dictionayItem);

                //提交
                context.SaveChanges();
            }
        }

        public static void UpdateDictionaryItem(DictionaryItemListRow data)
        {
            using (var context = new SausageEntities())
            {
                var item = context.DictionayItem.FirstOrDefault(dictionayItem => dictionayItem.ItemID == data.ItemID);
                if(item == null)
                    return;

                item.ItemName = data.ItemName;
                item.ItemPinYin = data.ItemPinYin;
                item.ItemValue = data.ItemValue;
                item.TheOrder = data.TheOrder;
                context.SaveChanges();
            }
        }

        public static DictionayItem GetDictionaryItemByID(Guid id)
        {
            using (var context = new SausageEntities())
            {
                return context.DictionayItem.FirstOrDefault(dictionayItem => dictionayItem.ItemID == id);
            }

        }

        public static DictionayItem GetDictionaryItemByName(string name)
        {
            using (var context = new SausageEntities())
            {
                return context.DictionayItem.FirstOrDefault(dictionayItem => dictionayItem.ItemName == name);
            }

        }
    }
}
